package com.itcast.tanhua.dubbo.server.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @PachageName: com.itcast.tanhua.dubbo.server
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/2314:31
 * @Version 1.0
 */
@Service
public class IdService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public Long createId(String type, String value) {

        String redisHashKey = "TANHUA_ID_HASH_" + type;
        if (this.redisTemplate.opsForHash().hasKey(redisHashKey,value)){
            return Long.valueOf(this.redisTemplate.opsForHash().get(redisHashKey,value).toString());
        }

        String idKey = "TANHUA_ID_" + type;
        Long increment = this.redisTemplate.opsForValue().increment(idKey);
        this.redisTemplate.opsForHash().put(redisHashKey,value,increment.toString());
        return increment;
    }
}
